<?php

namespace HuaweiVod;

use HuaweiCloud\SDK\Core\Auth\BasicCredentials;
use HuaweiCloud\SDK\Core\Http\HttpConfig;
use HuaweiCloud\SDK\Vod\V1\Model\AssetInfo;
use HuaweiCloud\SDK\Vod\V1\Model\ShowAssetMetaRequest;
use HuaweiCloud\SDK\Vod\V1\Model\UploadMetaDataByUrlReq;
use HuaweiCloud\SDK\Vod\V1\Model\UploadMetaDataByUrlRequest;
use HuaweiCloud\SDK\Vod\V1\VodClient;
use Monolog\Logger;

class HuaweiVod
{
    // 实例
    protected static $instance;

    protected static $ak = 'SBSJWMO4WRNDRTMWY73M';
    protected static $sk = 'LIJA1copK8UBAsekXzZPKPBiYTgJy7cfygE02KZm';
    protected static $domain = 'hid_7s_5kvj4tf0dtmq';
    protected static $domainId = 'b79ca8d387c74ceb90e9c7e0338fd18d';
    protected static $projectId = 'c389067ddc1942c892f00a077ee0b1e1'; // cn-north-4 华北-北京四
    protected static $endpoint = 'https://vod.cn-north-4.myhuaweicloud.com'; // 华北-北京四

    protected $client;

    /**
     * 构造函数
     * @access public
     * @param $ak
     * @param $sk
     * @param $projectId
     * @param $endpoint
     */
    public function __construct($ak, $sk, $projectId, $endpoint)
    {
        $config = HttpConfig::getDefaultConfig();
        $config->setIgnoreSslVerification(true);
        $credentials = new BasicCredentials($ak, $sk, $projectId);
        $this->client = VodClient::newBuilder()
            ->withHttpConfig($config)
            ->withEndpoint($endpoint)
            ->withCredentials($credentials)
            ->withStreamLogger($stream = 'php://stdout', $logLevel = Logger::INFO)
            ->withFileLogger($logPath = './test_log.txt', $logLevel = Logger::INFO)
            ->build();
    }

    public static function make()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self(self::$ak, self::$sk, self::$projectId, self::$endpoint);
        }
        return self::$instance;
    }

    /**
     * url拉取媒资
     * @param $url
     * @param $title
     * @param string $videoType
     * @return mixed
     */
    public function uploadByUrl($url, $title, $videoType = 'MP4')
    {
        $uploadMeta = [
            'video_type' => $videoType,
            'title' => $title,
            'url' => $url,
            'template_group_name' => 'system_template_group'
        ];
        $uploadMetas [] = $uploadMeta;
        $body = new UploadMetaDataByUrlReq(['uploadMetadatas' => $uploadMetas]);
        $request = new UploadMetaDataByUrlRequest(['body' => $body]);
        $result = $this->client->uploadMetaDataByUrl($request);
        return $result;
    }

    /**
     * 查询媒资详情
     * @param $assetIds
     * @return mixed
     */
    public function assetInfo($assetIds)
    {
        if (!is_array($assetIds)) {
            $assetIds = [$assetIds];
        }
        $request = new ShowAssetMetaRequest(['assetId' => $assetIds]);
        $result = $this->client->showAssetMeta($request);
        return $result;
    }
}